<%/**
			 * Find a Friend Confirmation.jsp
			 * 
			 * Validates a user's friend request
			 *
			 * @author Samson
			 */%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@page import="b1w1nn1ng.crossf1t.gui.*"%>
<%@page import="b1w1nn1ng.crossf1t.business.*"%>
<%@page import="b1w1nn1ng.crossf1t.db.*"%>

<%
	User user = (User) session.getAttribute("user");
	session.setAttribute("user", user);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<title>Find a Friend</title>
		<jsp:include page="/detectBrowser.jsp" />	
	</head>
	<body>
	
		<div id="container">
		<h1>Find a Friend</h1> <hr />
		<%
		
			StringBuilder errormsg = new StringBuilder();
			int temp;
			try {
					
				if (User.getUserByFriendCode(Integer.parseInt(request
						.getParameter("secretNumber").trim())) == null) {
					//System.out.println("PENIS MUNCH in friendsFindConfirmation.jsp");
					errormsg.append("<li>There is no user corresponding to the secret number. Please try again.</li>");
				}
			} catch (Exception ex) {//the secretNumber isn't all numbers!!!
				errormsg.append("Please input a secret number. There should be no letters or symbols. Please try again.");
			}
			
			if(errormsg.length()>0)
			{
				out.println(errormsg);
				out.println("<br/>");
				out.println("<input type=\"button\" value=\"Back\" onclick=\"window.location =\'friendsFind.jsp\'\"/>");
			}else{
				Integer friendCode = Integer.parseInt(request.getParameter("secretNumber").trim());
				System.out.println("friendcode: " + friendCode);
				User friend = User.getUserByFriendCode(friendCode);
				String firstName = request.getParameter("firstName");
				String lastName = request.getParameter("lastName");
				
				//check if BOTH the first and last name correspond to the friend's secret number
				if(!(friend.getFirstName().equals(firstName)&&friend.getLastName().equals(lastName)))
				{
					errormsg.append("The friend code does not correspond with the friend's first and last name");
				}
			//If we don't have any errors, then we add the friend to the user's friend list.
			User friend_temp = User.getUserByFriendCode(Integer.parseInt(request
					.getParameter("secretNumber").trim()));
			user.addFriend(friend_temp.getUsername());
			friend_temp.addFriend(user.getUsername());
			friend_temp = Database.updateUser(friend_temp);
			response.sendRedirect("home.jsp");
			}
		%>
		
		</div>
	</body>
</html>

